package com.thiswhyme.hystrix.server2.controller;

import com.thiswhyme.common.expection.BusinessException;
import com.thiswhyme.common.response.ResponseVo;
import com.thiswhyme.hystrix.server2.dto.UserDto;
import com.thiswhyme.hystrix.server2.feign.UserFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by thiswhyme on 2020/8/16.
 */
@RestController
@RequestMapping("/user2/")
public class UserController {

    @Autowired
    private UserFeignService userFeignService;

    @GetMapping("user-list")
    public ResponseVo<List<UserDto>> listUserInfo() {
        return userFeignService.listUsers();
    }

    @GetMapping("user-name")
    public ResponseVo<String> queryUserName(@RequestParam Integer userId) throws InterruptedException {
        if(0 == userId) {
            throw new IllegalArgumentException("userId应当大于0");
        } else if(1 == userId) {
            throw new BusinessException("userId为1的用户不存在");
        } else if(2 == userId) {
            Thread.sleep(2000);
        }
        return userFeignService.queryUserName(userId);
    }

    @GetMapping("user-name2")
    public ResponseVo<String> queryUserName2(@RequestParam Integer userId) throws InterruptedException {
        if(0 == userId) {
            throw new IllegalArgumentException("userId应当大于0");
        } else if(1 == userId) {
            throw new BusinessException("userId为1的用户不存在");
        } else if(2 == userId) {
            Thread.sleep(7000);
        }
        return ResponseVo.success(userFeignService.queryUserName(userId).getData());
    }
}
